package tk.leafotto.ssm.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.leafotto.ssm.beans.AlertInfo;
import tk.leafotto.ssm.beans.CamInfo;
import tk.leafotto.ssm.beans.Dvrinfo;
import tk.leafotto.ssm.service.AlertInfoService;
import tk.leafotto.ssm.service.CamInfoService;
import tk.leafotto.ssm.service.DvrinfoService;

import java.sql.Timestamp;

@Controller
public class GetInfoController {

    /**
     * DVR列表部分
     */
    @Autowired
    DvrinfoService dvrinfoService;

    @ResponseBody
    @RequestMapping("/getdvrinfo")
    public String getdvrinfo(){
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try {
            res = mapper.writeValueAsString(dvrinfoService.getDvrInfo());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(res);
        return res;
    }

    @ResponseBody
    @RequestMapping("/putdvrinfo/{dvrcode}/{desc}/{ip}/{port}/{name}/{pwd}/{dcn}")
    public String putdvrinfo(@PathVariable("dvrcode") String dvrcode, @PathVariable("desc") String desc, @PathVariable("ip") String ip, @PathVariable("port") Integer port, @PathVariable("name") String name, @PathVariable("pwd") String pwd, @PathVariable("dcn") int dcn){
        System.out.println(desc);
        Dvrinfo dvrinfo = new Dvrinfo();
        dvrinfo.setDvrcode(dvrcode);
        dvrinfo.setDesc(desc);
        dvrinfo.setIp(ip);
        dvrinfo.setPort(port);
        dvrinfo.setName(name);
        dvrinfo.setPwd(pwd);
        dvrinfo.setDcn(dcn);
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try {
            res = mapper.writeValueAsString(dvrinfoService.putDvrInfo(dvrinfo));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("/remdvrinfo/{id}")
    public String remdvrinfo(@PathVariable("id") Integer id){
        return dvrinfoService.remdvrinfo(id) + "";
    }

    /**
     * 告警信息部分
     */
    @Autowired
    AlertInfoService alertInfoService;
    @ResponseBody
    @RequestMapping("/getalertinfo/{id}")
    public String getalertinfo(@PathVariable("id") Integer id){
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try{
            res = mapper.writeValueAsString(alertInfoService.selectAlertInfo(id));
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("/getalertinfo")
    public String getalertinfo(){
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try{
            res = mapper.writeValueAsString(alertInfoService.getAlertInfo());
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("/getAlertNoConfirm")
    public String getAlertNoConfirm(){
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try{
            res = mapper.writeValueAsString(alertInfoService.getAlertNoConfirm());
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("/getalertdetail/{camid}/{alertTimeStart}/{alertTimeEnd}/{confirmstate}/{confirmuser}/{confirmTimeStart}/{confirmTimeEnd}/{remarks}/{startRow}/{limitRow}/{orderBy}/{desc}")
    public String getAlertDetail(@PathVariable("camid") Integer camid,
                                 @PathVariable("alertTimeStart") Long alertTimeStart,
                                 @PathVariable("alertTimeEnd") Long alertTimeEnd,
                                 @PathVariable("confirmstate") Integer confirmstate,
                                 @PathVariable("confirmuser") Integer confirmuser,
                                 @PathVariable("confirmTimeStart") Long confirmTimeStart,
                                 @PathVariable("confirmTimeEnd") Long confirmTimeEnd,
                                 @PathVariable("remarks") String remarks,
                                 @PathVariable("startRow") Integer startRow,
                                 @PathVariable("limitRow") Integer limitRow,
                                 @PathVariable("orderBy") String orderBy,
                                 @PathVariable("desc") Integer desc) {
        if("0".equals(remarks)){
            remarks = "";
        }
        if("0".equals(orderBy)){
            orderBy = "id";
        }
        if(limitRow == 0){
            limitRow = 1000;
        }
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try{
            res = mapper.writeValueAsString(alertInfoService.getAlertDetail(camid, alertTimeStart, alertTimeEnd, confirmstate, confirmuser, confirmTimeStart, confirmTimeEnd, remarks, startRow, limitRow, orderBy, desc));
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 增加告警信息
     * @param dvrid
     * @param camid
     * @param picname
     * @return 告警信息ID
     */
    @ResponseBody
    @RequestMapping("/putalertinfo/{dvrid}/{camid}/{picname}/{remarks}")
    public String putalertinfo(@PathVariable("dvrid") Integer dvrid, @PathVariable("camid") Integer camid, @PathVariable("picname") String picname, @PathVariable("remarks") String remarks){
        AlertInfo alertInfo = new AlertInfo();
        alertInfo.setDvrid(dvrid);
        alertInfo.setCamid(camid);
        alertInfo.setPicname(picname);
        alertInfo.setRemarks(remarks);
        return alertInfoService.putAlertInfo(alertInfo) + "";
    }

    /**
     * 确认告警
     * @param id
     * @param confirmuser
     * @return 1=success,0=failed
     */
    @ResponseBody
    @RequestMapping("/confirmalert/{id}/{confirmuser}/{remarks}")
    public String confirmalert(@PathVariable("id") Integer id,@PathVariable("confirmuser") Integer confirmuser, @PathVariable("remarks") String remarks){
        AlertInfo alertInfo = new AlertInfo();
        alertInfo.setId(id);
        alertInfo.setConfirmuser(confirmuser);
        alertInfo.setRemarks(remarks);
        return alertInfoService.confirmalert(alertInfo) + "";
    }

    @Autowired
    CamInfoService camInfoService;

    /**
     * 获取全部摄像机信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getcaminfo")
    public String getcaminfo(){
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try{
            res = mapper.writeValueAsString(camInfoService.getCamInfo());
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("/getcaminfo/{id}")
    public String getcaminfo(@PathVariable("id") Integer id){
        ObjectMapper mapper = new ObjectMapper();
        String res = "";
        try{
            res = mapper.writeValueAsString(camInfoService.getCamInfo(id));
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 添加摄像机信息
     * @param dvrid
     * @param name
     * @param groupid
     * @return 1=success，0=failed
     */
    @ResponseBody
    @RequestMapping("/putcaminfo/{camcode}/{dvrid}/{dvrcode}/{chansn}/{dchann}/{name}/{groupid}")
    public String putcaminfo(@PathVariable("camcode") String camcode, @PathVariable("dvrid") Integer dvrid, @PathVariable("dvrcode") String dvrcode, @PathVariable("chansn") Integer chansn, @PathVariable("dchann") Integer dchann, @PathVariable("name") String name, @PathVariable("groupid") Integer groupid){
        CamInfo camInfo = new CamInfo();
        camInfo.setCamcode(camcode);
        camInfo.setDvrid(dvrid);
        camInfo.setDvrcode(dvrcode);
        camInfo.setChansn(chansn);
        camInfo.setDchann(dchann);
        camInfo.setName(name);
        camInfo.setGroupid(groupid);
        return camInfoService.putCamInfo(camInfo) + "";
    }

    /**
     * 修改允许告警时间
     * @param id
     * @param allowtime
     * @return 1=success，0=failed
     */
    @ResponseBody
    @RequestMapping("/setallowtime/{id}/{allowtime}")
    public String setallowtime(@PathVariable("id") Integer id, @PathVariable("allowtime") Long allowtime){
        CamInfo camInfo = new CamInfo();
        camInfo.setId(id);
        Timestamp timestamp = new Timestamp(allowtime);
        camInfo.setAllowtime(timestamp);
        return camInfoService.setAllowTime(camInfo) + "";
    }
}
